<?php
class xml_table
    {
    public $tableName;
    public $tableCaption;
    public $scaffolding = 0;

    public $csvSource = 0;
    public $searchForm = 0;

    public $tableOrderby = "";

    public $treeTable = 0;

    public $urlTable = 0;
    public $backupTable = 0;
    public $urlField;

    public $userTable = 0;
    public $sortableTable =0;
    public $fileTable = 0;
    public $nmTable = 0;
    public $externalTable;
    public $systemTable = 0;

    public $usergroup;
    public $uniqueIndex = "";

    public $xmlFilename;
    public $baseDir = false;

    
        public $xmlField;  
    
    
  //  private $xml;

    //private $xml

  /*  private function readXml()
        {
        $xmlFilename=$this->xmlFilename;

        if ($xmlFilename == "")
            {
            $this->baseDir=false;
            $xmlFilename=config::$projectPath . "/db/db.xml";
            }

        if ($this->baseDir)
            {
            $xmlFilename=config::$basePath . $xmlFilename;
            }

        //$xml = simplexml_load_file($this->filename);

        /*if ($this->xmlString !== "")
            {
            $this->xml=simplexml_load_string($this->xmlString);
            }
        else
            {
            $this->xml=simplexml_load_file($xmlFilename);
            } */

    /*    $this->xml=simplexml_load_file($xmlFilename);
        }*/

 //   private function writeXml() { file::createTextFile($this->xmlFilename, $this->xml->saveXML(), $this->baseDir); }

    function createTable()
        {

        //      function createTable($table, $scaffolding = false)
        //  {
        //global $projectPath;

        //$filename  ="$projectPath/db/table.xml";

        //global $rootDir;

        //$xml       =simplexml_load_file($rootDir . $filename);

        $xmlFile = new xml_file();
        $xmlFile->readXml();
        
        
        //$table=$this->tableCaption;
        //$caption=$table;
        //$table=convert::convertToMachineCode($table);

        if ($this->tableCaption == "") {
            $this->tableCaption = $this->tableName;
        }
        
        $this->tableName=convert::convertToMachineCode($this->tableName);   
        
        
        
        
        
       /* if ($xmlFile->xml == NULL)
            {
            $this->readXml();
            }*/

        $nodes=$xmlFile->xml->xpath('/config/table[@name="' . $this->tableName . '"]');

        if (!empty($nodes))
            {
            //printf('At least one building named "%s" found', $name);

            }
        else
            {
            //printf('No building named "%s" found', $name);
            $tableChild=$xmlFile->xml->addChild("table", "");
            $tableChild->addAttribute("name", $this->tableName);
            $tableChild->addAttribute("caption", $this->tableCaption);

            if ($this->scaffolding)
                {
                $tableChild->addAttribute("scaffolding", "true");
                }
            }

            $xmlFile->writeXml();
            
            mysql::createTable($this->tableName);

            if ($this->scaffolding) {
                        $scaffolding = new scaffolding_administration();
                         $backendScaffolding->xmlFilename = config::$projectPath . "/db/db.xml";
        $backendScaffolding->baseDir = false;
                        $scaffolding->createBackend($this->tableName);
            }
            
            
        /*      $scaffoldingAdministration = new scaffolding_administration();
                 
                 
             //    $scaffoldingAdministration->xmlDatabase
             $scaffoldingAdministration->createAdministration()  */


        //file::createTextFile($filename, $xml->saveXML());
        //return $table;
        return $this->tableName;
        }
        
        
        
        
         function getFieldTyp($field)
        {
            
            
            
            $field    =strtolower($field);

        $fieldType="";

        if (isset($this->xmlField[$field]))
            {
            $fieldType=$this->xmlField[$field]->fieldType;
            }
        else
            {
            // echo "not exist $field";
            }


        /*foreach ($this->xmlField as $xmlFieldItem) {
            
            
            //echo 
            
            if ($xmlFieldItem->fieldName == $field) {
                $fieldType = $xmlFieldItem->fieldType;
                
            }
            
        }  */

        return $fieldType;
            
            
            
         return "";   
            
        }
        
        
        
        
        
        
        
    }
?>